#define FAKE_INPUT
using UnityEngine;
using System.Collections;

public class IMTouch
{
	public int fingerId;
	public Vector2 position;
	public float deltaTime;
	public TouchPhase phase;
	public int tapCount;
	public Vector2 deltaPosition;
}

public delegate bool TouchCallback( IMTouch _t );

public class InputManager
{
	//=======================================================================
	public TouchCallback BeginTouch{get;set;}
	public TouchCallback MoveTouch{get;set;}
	public TouchCallback EndedTouch{get;set;}
	
	//-----------------------------------------------------------------------
	#if (FAKE_INPUT)
	Vector2 lastMousePosition;
	#endif
	private bool m_Focused;
	//bool moveTest = false;
	
	//=======================================================================
	public InputManager()
	{
		m_Focused = false;
		//moveTest = false;
	}
	
	public InputManager( TouchCallback _b, TouchCallback _m, TouchCallback _e )
	{
		m_Focused = false;
		BeginTouch = _b;
		MoveTouch = _m;
		EndedTouch = _e; 
	}
	public void Proccess()
	{
#if (FAKE_INPUT)
		if (Input.GetMouseButton(0) || Input.GetMouseButtonUp(0) ) {
			IMTouch _touch = new IMTouch();
			_touch.fingerId = 10;
			_touch.position = new Vector2( Input.mousePosition.x, Input.mousePosition.y );
			_touch.deltaTime = Time.deltaTime;
			
			if( Input.GetMouseButtonDown(0) ){
				_touch.deltaPosition = new Vector2(0f,0f);
			}else{
				_touch.deltaPosition = _touch.position - lastMousePosition;
				lastMousePosition = _touch.position;
			}
			
			_touch.phase =  ( Input.GetMouseButtonDown(0) ? TouchPhase.Began : 
							( Input.GetMouseButtonUp(0) ? TouchPhase.Ended : _touch.deltaPosition.sqrMagnitude > 1f ? TouchPhase.Moved : TouchPhase.Stationary) );
			_touch.tapCount = 1;
			
#else
			if(Input.touchCount > 0 )
			{
				Touch _rTouch = Input.touches[0];
				IMTouch _touch = new IMTouch();
				_touch.fingerId = _rTouch.fingerId;
				_touch.position = _rTouch.position;
				_touch.deltaTime = _rTouch.deltaTime;
				_touch.deltaPosition = _rTouch.deltaPosition;
				_touch.phase = _rTouch.phase;
				_touch.tapCount = _rTouch.tapCount;
#endif
				if( _touch.phase == TouchPhase.Began )
				{
					if( BeginTouch != null )
					{
						//moveTest = false;
						if( BeginTouch( _touch ) )
							m_Focused = true;
					}
					
				}else if(m_Focused){
					
					switch(_touch.phase)
					{
					case TouchPhase.Moved:
					//moveTest = true;
						if( MoveTouch != null )
							MoveTouch( _touch );
						break;
						
					case TouchPhase.Canceled:
					case TouchPhase.Ended:
						if( EndedTouch != null )
							EndedTouch( _touch );						
						m_Focused = false;
						break;
						
						//m_Focused = false;
						//break;
					};
				}
			}
		}
	}